package com.example.sunsg.okhttptest.activity;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.breadtrip.R;
import com.example.sunsg.okhttptest.bean.Animations;

public class SunsgActivity extends Activity implements View.OnClickListener {
    private LinearLayout mLlPlueAnimation;
    private ImageView oldTripIv;
    private ImageView storyIv;
    /**动画时间 300 ms*/
    private static final int ANIMATION_DURATION = 300;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sunsg);
        initView();
    }

    private void initView() {
        Button btnRecomment = ((Button) findViewById(R.id.btn_recomment));
        btnRecomment.setOnClickListener(this);

        Button btnCityHunter = ((Button) findViewById(R.id.btn_city_hunter));
        btnCityHunter.setOnClickListener(this);

        Button btnIndependendTravel = ((Button) findViewById(R.id.btn_independend_travel));
        btnIndependendTravel.setOnClickListener(this);

        Button btnMe = ((Button) findViewById(R.id.btn_me));
        btnMe.setOnClickListener(this);

        ImageView imageView = ((ImageView) findViewById(R.id.plue));
        imageView.setOnClickListener(this);

        //plue 动画
        mLlPlueAnimation = (LinearLayout) findViewById(R.id.ll_container);
        mLlPlueAnimation.setOnClickListener(this);
        oldTripIv = (ImageView) findViewById(R.id.iv_long_trip);
        oldTripIv.setOnClickListener(this);

        storyIv = (ImageView) findViewById(R.id.iv_life_story);
        storyIv.setOnClickListener(this);

        hidePlueAnimation(0);

    }

    /**
     * 显示点击plue动画
     */
    private void showPlueAnimation(int duration){
        showPlueAnimation(oldTripIv,duration);
        showPlueAnimation(storyIv, duration);
        mLlPlueAnimation.animate().alpha(0).alpha(1).setDuration(300).start();
    }

    /**
     * 显示点击plue动画
     */
    private void showPlueAnimation(View view,int duration) {
        ObjectAnimator alpher = ObjectAnimator.ofFloat(view, Animations.ANIMATION_ALPHA, 0, 1);
        ObjectAnimator rotation = ObjectAnimator.ofFloat(view, Animations.ANIMATION_ROTATION, 0, 360);
        ObjectAnimator scalX = ObjectAnimator.ofFloat(view, Animations.ANIMATION_SCALE_X, 0, 1);
        ObjectAnimator scalY = ObjectAnimator.ofFloat(view, Animations.ANIMATION_SCALE_Y, 0, 1);
        ObjectAnimator translateY = ObjectAnimator.ofFloat(view, Animations.ANIMATION_TRANSLATION_Y, 600, 0);

        AnimatorSet set = new AnimatorSet();
        set.playTogether(alpher, rotation, scalX, scalY, translateY);
        set.setDuration(duration);
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {
                mLlPlueAnimation.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAnimationEnd(Animator animation) {

            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        set.start();

    }

    /**
     * 隐藏 plue动画
     */
    private void hidePlueAnimation(int duration){
        hidePlueAnimation(oldTripIv,duration);
        hidePlueAnimation(storyIv, duration);
        mLlPlueAnimation.animate().alpha(1).alpha(0).setDuration(duration).start();
    }

    /**
     * 隐藏 plue动画
     */
    private void hidePlueAnimation(View view,int duration) {
        ObjectAnimator alpher = ObjectAnimator.ofFloat(view, Animations.ANIMATION_ALPHA, 1, 0);
        ObjectAnimator rotation = ObjectAnimator.ofFloat(view, Animations.ANIMATION_ROTATION, 0, -360);
        ObjectAnimator scalX = ObjectAnimator.ofFloat(view, Animations.ANIMATION_SCALE_X, 1, 0);
        ObjectAnimator scalY = ObjectAnimator.ofFloat(view, Animations.ANIMATION_SCALE_Y, 1, 0);
        ObjectAnimator translateY = ObjectAnimator.ofFloat(view, Animations.ANIMATION_TRANSLATION_Y, 0,600);

        AnimatorSet set = new AnimatorSet();
        set.playTogether(alpher, rotation, scalX, scalY, translateY);
        set.setDuration(duration);
        set.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mLlPlueAnimation.setVisibility(View.GONE);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        set.start();
    }

    /**
     * 添加老游记
     */
    private void jumpToAddOldTrip() {

    }

    /**
     * 添加故事集
     */
    private void jumpToAddStorys() {
        LifeStoryActivity.launch(SunsgActivity.this);

    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            /**推荐*/
            case R.id.btn_recomment:

                break;
            /**cityhunter*/
            case R.id.btn_city_hunter:

                break;
            /**自由行*/
            case R.id.btn_independend_travel:

                break;
            /**我的*/
            case R.id.btn_me:

                break;
            /**添加游记或故事*/
            case R.id.plue:
                showPlueAnimation(ANIMATION_DURATION);
                break;
            /**添加老友记*/
            case R.id.iv_long_trip:
                jumpToAddOldTrip();
                break;
            /**添加故事集*/
            case R.id.iv_life_story:
                jumpToAddStorys();
                break;
            case R.id.ll_container:
                hidePlueAnimation(ANIMATION_DURATION);
                break;
        }
    }
}
